<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2019-06-12
 * Time: 17:28
 */

namespace extend\wechat\mp;


use App\Models\MpReplies;

class Message
{
    /**
     * @param $msg
     * @return string
     */
    public static function textMsg($msg)
    {
        $replies = MpReplies::getOne(['type' => 'keyword_reply', 'status' => '1', 'keyword' => $msg['Content']]);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function imageMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'image', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function voiceMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'voice', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function videoMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'video', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function shortvideoMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'short_video', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function locationMsg($msg)
    {
//        $reply = self::afterMsg($msg);
//        if ($msg['MsgType'] == 'event') {
//        } else {
//        }
        $replies =  MpReplies::getOne(['type' => 'location', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function linkMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'link', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function subscribeMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'subscribe', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function scanMsg($msg)
    {
        $replies = [];
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function unsubscribeMsg($msg)
    {
        $replies =  MpReplies::getOne(['type' => 'unsubscribe', 'status' => '1']);
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function clickMsg($msg)
    {
        $replies = [];
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @return string
     */
    public static function viewMsg($msg)
    {
        $replies = [];
        return self::afterMsg($msg, $replies);
    }

    /**
     * @param $msg
     * @param $replies
     * @return string
     */
    private static function afterMsg($msg, $replies)
    {
        if (empty($replies->resource)) {
            return self::defaultMsg($msg);
        }

        $type = $replies->resource->type;
        $content = json_decode($replies->resource->content, true);

        $reply['fromUsername'] = $msg['FromUserName'];
        $reply['toUsername'] = $msg['ToUserName'];
        $reply['time'] = time();

        switch ($type) {
            case 'text':
                $reply['msg'] = $content['text'];
                $reply = Format::text($reply);
                break;
            case 'image':
                $reply['mediaID'] = $content['media_id'];
                $reply = Format::image($reply);
                break;
            case 'voice':
                $reply['mediaID'] = $content['media_id'];
                $reply = Format::voice($reply);
                break;
            case 'video':
                $reply['title'] =$content['title'];
                $reply['description'] =$content['describe'];
                $reply['mediaID'] = $content['media_id'];
                $reply = Format::video($reply);
                break;
            case 'music':
                $reply['title'] = $content['title'];
                $reply['description'] = $content['describe'];
                $reply['musicURL'] = url($content['music_url']);
                $reply['hQMusicUrl'] = url($content['music_url']);
                $reply['thumbMediaId'] = $content['cover_media_id'];
                $reply = Format::music($reply);
                break;
            case 'news':
                foreach ($content as $key => $val) {
                    $reply["articleCount"][$key]["title"] = $val["title"];
                    $reply["articleCount"][$key]["description"] = $val["describe"];
                    $reply["articleCount"][$key]["picUrl"] = url($val["cover"]);
                    $reply["articleCount"][$key]["url"] = $val["url"];
                }
                $reply = Format::news($reply);
                break;
            default:
                $reply = self::defaultMsg($msg);
                break;
        }
        return $reply;
    }


    /**
     * @param $msg
     * @return string
     */
    private static function defaultMsg($msg)
    {
        $replies = MpReplies::getOne(['type' => 'auto_reply', 'status' => '1']);
        if (empty($replies)) {
            $reply = 'SUCCESS';
        } else {
            $reply = self::afterMsg($msg, $replies);
        }
        return $reply;
    }
}